package df.servlet.process;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import df.bean.db.conn.DBConn;
import df.bean.db.conn.DBConnection;
import df.bean.db.table.PaymentMonthly;
import df.bean.obj.util.JDate;
import df.bean.obj.util.Variables;

public class ProcessCalPaymentSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private PaymentMonthly pm = null;
    private DBConnection connect = null;
    private int countNum = 0;
    private int maxSize = 0;

	public void processRequest(HttpServletRequest request, HttpServletResponse response) {
		String mode = request.getParameter("mode");
	    System.out.println(Variables.IS_TEST ? "Mode : "+mode : "");
		if(request.getParameter("mode") != null){
			if(mode.equals("paymentSummmary")){
				String date = request.getParameter("date");
				String hospitalCode = request.getParameter("hospitalCode");
				String doctorCode = request.getParameter("doctorCode");
				int count = Integer.parseInt(request.getParameter("count"));
				String bothJson= "";
				System.out.println(Variables.IS_TEST ? "Count : "+count : "");
				if(count == 0){
					connect = new DBConnection();
				    connect.connectToLocal();
				    pm = new PaymentMonthly(connect);
				    countNum = 0;
				    this.maxSize = Integer.parseInt(request.getParameter("maxSize"))-1;
				}

                if (!Calculate(hospitalCode, doctorCode, JDate.saveDate(date))) {
                	
                }else{
                	countNum++;
                }
                bothJson = "{\"count\" : \""+countNum+"\",\"lastData\" : \"N\"}";
				if(count == this.maxSize){
					connect.Close();
					pm = null;
					this.maxSize = 0;
			        bothJson = "{\"count\" : \""+countNum+"\",\"lastData\" : \"Y\"}";
				}
				
				try {
			        response.setContentType("application/json"); 
					response.setCharacterEncoding("utf-8"); 
					response.getWriter().write(bothJson);
				} catch (IOException e) {
					System.out.println("Error Message = > " + e.getMessage());
				}
                
			}else if(mode.equals("getDoctor")){
				String hospitalCode = request.getParameter("hospitalCode");
				DBConn dbConn = new DBConn();
				String bothJson= "{ \"listDoctorCode\" : [";
				String sql = "";
				HashMap<String, String> hashData = new HashMap<String, String>();
		        sql="SELECT CODE FROM DOCTOR WHERE HOSPITAL_CODE = '"+hospitalCode+"'";
		        ArrayList<HashMap<String, String>> arrData = dbConn.listQueryData(sql);
		        dbConn.closeDB("");
		        if(arrData.size() > 0){
		        	for(int i=0;i<arrData.size();i++){
		        		hashData = arrData.get(i);
		        		bothJson += "{\"doctorCode\" : \""+hashData.get("CODE")+"\"}, ";
		        	}
		        }
	    		try{
	    			bothJson = bothJson.substring(0, bothJson.length()-2);
					bothJson += "]}";
			        response.setContentType("application/json"); 
					response.setCharacterEncoding("utf-8"); 
					response.getWriter().write(bothJson);
				}catch(IOException e){
					System.out.println("Error Message => " + e.getMessage());
				}
			}
		}
		else{
			System.out.println("mode null don't work");
		}
	}

    public boolean Calculate(String hospitalCode, String doctorCode, String payDate) {
        boolean ret = false;
        if (pm.insertToPaymentSummmary(hospitalCode, payDate, doctorCode)>-1) {
            ret = true;    
        }else {  
            ret = false;   
        }
        return ret;
    } 
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
}